package org.takeback.chat.service;

import com.google.common.collect.ImmutableMap;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.takeback.chat.entity.PubExchangeLog;
import org.takeback.chat.entity.PubShop;
import org.takeback.chat.entity.PubUser;
import org.takeback.service.BaseService;
import org.takeback.util.exception.CodedBaseRuntimeException;

@Service
public class ShopService extends BaseService {
	@Transactional
	public List<PubShop> list(Integer pageNo, Integer pageSize) {
        return this.dao.findByHqlPaging("from PubShop order by sortNum desc ", pageSize, pageNo);
	}

	@Transactional
	public PubShop get(Integer id) {
        return this.dao.get(PubShop.class, id);
	}

	@Transactional
	public PubExchangeLog getContactInfo(Integer id) {
		List<PubExchangeLog> list = this.dao.findByHql("from PubExchangeLog where uid =:uid order by id desc",
				ImmutableMap.of("uid", id));
		if (list.size() == 0) {
			return null;
		}
		return (PubExchangeLog) list.get(0);
	}

	@Transactional(rollbackFor = { Throwable.class })
	public void doExchage(Integer uid, Integer shopId, String name, String address, String mobile) {
		PubShop s = (PubShop) this.dao.get(PubShop.class, shopId);
		if (s == null) {
			throw new CodedBaseRuntimeException("商品信息丢失");
		}
		String hql = "update PubUser set money = coalesce(money,0) - :money where money > :money and id = :uid";
		int effected = this.dao.executeUpdate(hql, ImmutableMap.of("money", s.getMoney(), "uid", uid));
		if (effected == 0) {
			throw new CodedBaseRuntimeException("账户金额不足!");
		}
		
		s.setExchanged(s.getExchanged() + 1);
		s.setStorage(s.getStorage() - 1);
		this.dao.save(PubShop.class, s);
		
		PubUser u = (PubUser) this.dao.get(PubUser.class, uid);
		PubExchangeLog pel = new PubExchangeLog();

		pel.setUid(uid);
		pel.setNickName(u.getUserId());
		pel.setExchangeTime(new Date());

		pel.setMobile(mobile);
		pel.setMoney(s.getMoney());
		pel.setName(name);
		pel.setAddress(address);

		pel.setShopId(shopId.toString());
		pel.setShopName(s.getName());
		pel.setStatus("0");
		this.dao.save(PubExchangeLog.class, pel);
	}
}
